namespace Pinwheel.Kernel
{
    public delegate ValueTask<IMessage> ApiMethod(ServiceMessage message);
    
    public enum ServiceStatus : byte
    {
        Invalid,
        Registered,
        Bootstrap,
        Alive,
        Stopped,
    }
    
    public interface IService
    {
        IServiceManager ServiceManager { get; }
        
        ServiceStatus Status { get; }
        
        string Alias { get; }
        
        void OnRegister(PID pid);
        
        void OnUnregister();
        
        void RegisterApi(string name, ApiMethod handler);
        
        void UnregisterApi(string name);
        
        ValueTask<IMessage> InvokeApi(string name, ServiceMessage message);
        

        #region MessageQueue
        
        IMessageQueue<ServiceMessage> MessageQueue { get; }
        void Post(ServiceMessage message);
        
        // void Send(PID dest, ServiceMessage message);
        //
        // Task<IMessage> Call(PID dest, ServiceMessage message);
        
        ValueTask<IMessage> ReceiveAsync(ServiceMessage message); 

        #endregion
    }
    
    public interface IServiceAgent : IClientAgent
    {
        void Connect();
    }
    
    public interface IServiceInstance : IService
    {
        void Create(IServiceManager serviceManager, IMessageQueue<ServiceMessage> messageQueue);
        
        int Init();
        
        void Signal(int signal);
        
        void Release();
    }
}
